package com.example.forum.controller;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.config.ContentConfig;
import com.example.forum.model.Article;
import com.example.forum.model.ArticleReply;
import com.example.forum.model.User;
import com.example.forum.service.imp.ArticleReplyServicImp;
import com.example.forum.service.imp.ArticleServiceImp;
import jakarta.annotation.Nullable;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@Slf4j
@RestController
@RequestMapping("reply")
public class ArticleReplyController {
    @Autowired
    private ArticleReplyServicImp articleReplyServicImp;

    @Autowired
    private ArticleServiceImp articleServiceImp;

    @RequestMapping("create")
    public AppResult create(HttpServletRequest request,
                            @RequestParam("articleId") @NonNull Long articleId,
                            @RequestParam("content") @NonNull String content){
        //获取用户的状态
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(ContentConfig.USER_SESSION);
        //用户是否已经被禁言了
        if(user.getState() == 1){
            return AppResult.fail(ResultCode.FAILED_USER_BANNED);
        }

        //获取帖子的状态
        Article article = articleServiceImp.selectById(articleId);
        if (article == null || article.getDeleteState() == 1) {
            // 表示已删除或不存在
            return AppResult.fail(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        // 是否封帖
        if (article.getState() == 1) {
            // 表示已封贴
            return AppResult.fail(ResultCode.FAILED_ARTICLE_BANNED);
        }
        //构造传递的对象
        ArticleReply insertArticleReply = new ArticleReply();
        insertArticleReply.setArticleId(articleId);//传递的帖子id
        insertArticleReply.setPostUserId(user.getId());//用户发送帖子对应的id
        insertArticleReply.setContent(content);

        articleReplyServicImp.create(insertArticleReply);
        //返回成功
        return AppResult.success();

    }
    @RequestMapping("/getReplies")
    public AppResult<List<ArticleReply>> selectByArticleId(@RequestParam("articleId") @NonNull Long articleId){
        if(articleId <= 0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            return AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE);
        }

        //查询文章
        Article article = articleServiceImp.selectById(articleId);

        //校验文章状态是否正确
        if(article == null || article.getDeleteState() == 1){
            //说明文章不存在或者已经被删除
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            return AppResult.fail(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        if(article.getState() == 1){
            log.warn(ResultCode.FAILED_ARTICLE_BANNED.toString());
            return AppResult.fail(ResultCode.FAILED_ARTICLE_BANNED);
        }

        //调用服务层，获取返回的回复列表信息
        List<ArticleReply> articleReplies = articleReplyServicImp.selectByArticleId(article.getId());

        //返回数据
        return AppResult.success(articleReplies);
    }
}
